//
//  CustomLayout.m
//  biaoqianTest
//
//  Created by 美鑫科技 on 16/7/26.
//  Copyright © 2016年 MorpLCP. All rights reserved.
//

#import "CustomLayout.h"

@interface CustomLayout ()
{
    NSInteger _cellCount;
}

@end

@implementation CustomLayout

const NSInteger kMaxCellSpacing = 10;

- (void)prepareLayout
{
    [super prepareLayout];
    _cellCount = [self.collectionView numberOfItemsInSection:0];
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    
    NSMutableArray<UICollectionViewLayoutAttributes *> *resultArray = [NSMutableArray array];
    NSArray *attrArray = [NSArray arrayWithArray:[[super layoutAttributesForElementsInRect:rect] copy]];
    for (UICollectionViewLayoutAttributes *attr in attrArray) {
        NSIndexPath *index = attr.indexPath;
        attr.frame = [self layoutAttributesForItemAtIndexPath:index].frame;
        if (attr.representedElementCategory == 0) {
            [resultArray addObject:attr];
        }
    }
    return resultArray;
}

//定义cell的布局
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.collectionView numberOfItemsInSection:indexPath.section] == 0) {
        return nil;
    }
    UICollectionViewLayoutAttributes* currentItemAttributes =
    [[super layoutAttributesForItemAtIndexPath:indexPath] copy];
    
    UIEdgeInsets sectionInset = [(UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout sectionInset];
    
    if (indexPath.item == 0) { // first item of section
        CGRect frame = currentItemAttributes.frame;
        frame.origin.x = sectionInset.left; // first item of the section should always be left aligned
        currentItemAttributes.frame = frame;
        
        return currentItemAttributes;
    }
    
    NSIndexPath* previousIndexPath = [NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section];
    CGRect previousFrame = [self layoutAttributesForItemAtIndexPath:previousIndexPath].frame;
    CGFloat previousFrameRightPoint = previousFrame.origin.x + previousFrame.size.width + kMaxCellSpacing;
    
    CGRect currentFrame = currentItemAttributes.frame;
    CGRect strecthedCurrentFrame = CGRectMake(0,
                                              currentFrame.origin.y,
                                              self.collectionView.frame.size.width,
                                              currentFrame.size.height);
    
    if (!CGRectIntersectsRect(previousFrame, strecthedCurrentFrame)) { // if current item is the first item on the line
        // the approach here is to take the current frame, left align it to the edge of the view
        // then stretch it the width of the collection view, if it intersects with the previous frame then that means it
        // is on the same line, otherwise it is on it's own new line
        CGRect frame = currentItemAttributes.frame;
        frame.origin.x = sectionInset.left; // first item on the line should always be left aligned
        currentItemAttributes.frame = frame;
        return currentItemAttributes;
    }
    
    CGRect frame = currentItemAttributes.frame;
//    frame.size.height = 60;
    frame.origin.x = previousFrameRightPoint;
    currentItemAttributes.frame = frame;
    return currentItemAttributes;
}

@end
